package com.zuijin.vue2blog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * --------------------------------------------
 * ClassName:    SwaggerConfig
 * CreateBy:     IntelliJ IDEA
 * Author:       醉瑾
 * Date:         2022-04-17
 * Description :
 * --------------------------------------------
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {
    // 所有接口（按照包位置分的组）
    @Bean
    public Docket createAllApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("vue2项目所有接口")
                .apiInfo(apiInfo())
                .select()
                //为当前包下所有controller生成API文档
                .apis(RequestHandlerSelectors.basePackage("com.zuijin.vue2blog.controller"))
                //为有@Api注解的Controller生成API文档
                //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //为有@ApiOperation注解的方法生成API文档
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //正则匹配请求路径，并分配至当前分组，当前所有接口
                .paths(PathSelectors.any())
                .build();
    }

    // 小分组 （按照路由分的组）
    @Bean
    public Docket createOssApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("Oss上传相关分组接口")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/oss/**"))
                .build()
                .pathMapping("/");
    }

    @Bean
    public Docket createUmsApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("文章相关分组接口")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/art/**"))
                .build()
                .pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 标题
                .title("vue2blog项目接口文档")
                // 描述
                .description("这是vue2blog项目的文档接口")
                // 服务条款
                //.termsOfServiceUrl("http://localhost:8080/")
                // 作者信息
                .contact(new Contact("醉瑾", "####", "1378511152@qq.com"))
                // 版本
                .version("1.0")
                .build();
    }
}
